package com.example.demo.module.purchase.controller;

import com.example.demo.common.entity.response.Rs;
import com.example.demo.constant.Constant;
import com.example.demo.entity.pojo.develop.DevelopBomPoJo;
import com.example.demo.entity.pojo.purchase.PurchaseListPoJo;
import com.example.demo.entity.pojo.purchase.PurchaseOrderPoJo;
import com.example.demo.entity.request.develop.DevelopBomRequest;
import com.example.demo.entity.request.purchase.PurchaseListRequest;
import com.example.demo.entity.response.purchase.PurchaseListResponse;
import com.example.demo.entity.response.purchase.PurchaseOrderResponse;
import com.example.demo.module.purchase.service.PurchaseListService;
import com.example.demo.utils.PoJoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author lzy
 * @date 2020-09-15 16:49:26
 */
@RestController
@RequestMapping("/purchase")
@Api(tags = "采购清单表接口")
public class PurchaseListController {
    @Autowired
    private PurchaseListService service;

    /**
     * 查询
     */
    @GetMapping()
    @ApiOperation(value = "查询清单", notes = "根据条件返回集合对象")
    public ResponseEntity<Rs<List<PurchaseListResponse>>> list(PurchaseListRequest request,
                                                               @ApiIgnore @RequestParam(value = Constant.HeaderKey.COMPANY_ID, required = false) Long companyId,
                                                               @RequestParam("state")Long state) {
        if(state==0) {
            PurchaseListPoJo poJo = new PurchaseListPoJo();
            BeanUtils.copyProperties(request, poJo);
            poJo.setCompanyId(companyId);
            List<PurchaseListPoJo> result = service.findList(poJo, request.getStartTime(), request.getEndTime());
            return Rs.ok(Rs.json().put("listPurchaseBomResponse", PoJoUtil.transToPoJo(result, PurchaseListResponse.class)));
        }else{
            PurchaseListPoJo poJo = new PurchaseListPoJo();
            BeanUtils.copyProperties(request, poJo);
            poJo.setCompanyId(companyId);
            List<PurchaseListPoJo> result = service.findList(poJo, request.getStartTime(), request.getEndTime());
            List<PurchaseListResponse> response=PoJoUtil.transToPoJo(result, PurchaseListResponse.class);

            Map<String,List<PurchaseListResponse>> map=response.stream().collect(Collectors.groupingBy(PurchaseListResponse::getSupplierName));
            return Rs.ok(Rs.json().put("listPurchaseBomResponse", map));
        }
    }



    /**
     * 批量添加
     */
    @PostMapping()
    @ApiOperation(value = "批量添加清单表", notes = "返回msg")
    public ResponseEntity<Rs<Object>> batchadd(@ApiParam(name = "List<PurchaseBomPoJo>", value = "多个清单表批量添加，需要json数组") @RequestBody List<PurchaseListRequest> request,
                                               @ApiIgnore @RequestParam(Constant.HeaderKey.COMPANY_ID) Long companyId) {
        service.createBatchCid(PoJoUtil.transToPoJo(request, PurchaseListPoJo.class), companyId);
        return Rs.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation(value = "修改清单表", notes = "返回msg")
    public ResponseEntity<Rs<Object>> update(@ApiParam(name = "Id", value = "根据id更新", required = true) @RequestBody PurchaseListResponse request,
                                             @ApiIgnore @RequestParam(Constant.HeaderKey.COMPANY_ID) Long companyId,
                                             @RequestParam("carbonCopy")String carbonCopy,
                                             @RequestParam("approval")String approval) {
        PurchaseListPoJo poJo = new PurchaseListPoJo();
        BeanUtils.copyProperties(request, poJo);
        poJo.setCompanyId(companyId);
        PurchaseOrderPoJo pj=new PurchaseOrderPoJo();
        //0未提交临时保存
        pj.setState(0l);
        pj.setApproval(approval);
        pj.setCarbonCopy(carbonCopy);
        service.update(poJo,pj);
        return Rs.ok();
    }

    /**
     * 修改并提交
     */
    @PutMapping("/Submit")
    @ApiOperation(value = "修改清单表以及订单表", notes = "返回msg")
    public ResponseEntity<Rs<Object>> submit(@ApiParam(name = "Id", value = "根据id更新", required = true) @RequestBody PurchaseListResponse request,
                                             @ApiIgnore @RequestParam(Constant.HeaderKey.COMPANY_ID) Long companyId,
                                             @RequestParam("carbonCopy")String carbonCopy,
                                             @RequestParam("approval")String approval) {
        PurchaseListPoJo poJo = new PurchaseListPoJo();
        BeanUtils.copyProperties(request, poJo);
        poJo.setCompanyId(companyId);
        PurchaseOrderPoJo pj=new PurchaseOrderPoJo();
        //1.审批中
        pj.setState(1l);
        pj.setApproval(approval);
        pj.setCarbonCopy(carbonCopy);
        service.update(poJo,pj);

        return Rs.ok();
    }


    /**
     * 批量删除
     */
    @DeleteMapping("/batch/{ids}")
    @ApiOperation(value = "批量删除清单表", notes = "返回msg")
    public ResponseEntity<Rs<Object>> batchdelete(@ApiParam(name = "ids", value = "根据ids删除清单表", required = true) @PathVariable("ids") List<Long> ids) {
        service.removeByIds(ids);
        return Rs.ok();
    }

}
